<?php


abstract class BaseStockdiary extends BaseObject  implements Persistent {


  const PEER = 'StockdiaryPeer';

	
	protected static $peer;

	
	protected $id;

	
	protected $datenew;

	
	protected $reason;

	
	protected $location;

	
	protected $product;

	
	protected $units;

	
	protected $price;

	
	protected $aLocations;

	
	protected $aProducts;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		parent::__construct();
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getDatenew($format = 'Y-m-d H:i:s')
	{
		if ($this->datenew === null) {
			return null;
		}


		if ($this->datenew === '0000-00-00 00:00:00') {
									return null;
		} else {
			try {
				$dt = new DateTime($this->datenew);
			} catch (Exception $x) {
				throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->datenew, true), $x);
			}
		}

		if ($format === null) {
						return $dt;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $dt->format('U'));
		} else {
			return $dt->format($format);
		}
	}

	
	public function getReason()
	{
		return $this->reason;
	}

	
	public function getLocation()
	{
		return $this->location;
	}

	
	public function getProduct()
	{
		return $this->product;
	}

	
	public function getUnits()
	{
		return $this->units;
	}

	
	public function getPrice()
	{
		return $this->price;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = StockdiaryPeer::ID;
		}

		return $this;
	} 
	
	public function setDatenew($v)
	{
						if ($v === null || $v === '') {
			$dt = null;
		} elseif ($v instanceof DateTime) {
			$dt = $v;
		} else {
									try {
				if (is_numeric($v)) { 					$dt = new DateTime('@'.$v, new DateTimeZone('UTC'));
															$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
				} else {
					$dt = new DateTime($v);
				}
			} catch (Exception $x) {
				throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x);
			}
		}

		if ( $this->datenew !== null || $dt !== null ) {
			
			$currNorm = ($this->datenew !== null && $tmpDt = new DateTime($this->datenew)) ? $tmpDt->format('Y-m-d H:i:s') : null;
			$newNorm = ($dt !== null) ? $dt->format('Y-m-d H:i:s') : null;

			if ( ($currNorm !== $newNorm) 					)
			{
				$this->datenew = ($dt ? $dt->format('Y-m-d H:i:s') : null);
				$this->modifiedColumns[] = StockdiaryPeer::DATENEW;
			}
		} 
		return $this;
	} 
	
	public function setReason($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->reason !== $v) {
			$this->reason = $v;
			$this->modifiedColumns[] = StockdiaryPeer::REASON;
		}

		return $this;
	} 
	
	public function setLocation($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->location !== $v) {
			$this->location = $v;
			$this->modifiedColumns[] = StockdiaryPeer::LOCATION;
		}

		if ($this->aLocations !== null && $this->aLocations->getId() !== $v) {
			$this->aLocations = null;
		}

		return $this;
	} 
	
	public function setProduct($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->product !== $v) {
			$this->product = $v;
			$this->modifiedColumns[] = StockdiaryPeer::PRODUCT;
		}

		if ($this->aProducts !== null && $this->aProducts->getId() !== $v) {
			$this->aProducts = null;
		}

		return $this;
	} 
	
	public function setUnits($v)
	{
		if ($v !== null) {
			$v = (double) $v;
		}

		if ($this->units !== $v) {
			$this->units = $v;
			$this->modifiedColumns[] = StockdiaryPeer::UNITS;
		}

		return $this;
	} 
	
	public function setPrice($v)
	{
		if ($v !== null) {
			$v = (double) $v;
		}

		if ($this->price !== $v) {
			$this->price = $v;
			$this->modifiedColumns[] = StockdiaryPeer::PRICE;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->datenew = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->reason = ($row[$startcol + 2] !== null) ? (int) $row[$startcol + 2] : null;
			$this->location = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
			$this->product = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
			$this->units = ($row[$startcol + 5] !== null) ? (double) $row[$startcol + 5] : null;
			$this->price = ($row[$startcol + 6] !== null) ? (double) $row[$startcol + 6] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 7; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Stockdiary object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aLocations !== null && $this->location !== $this->aLocations->getId()) {
			$this->aLocations = null;
		}
		if ($this->aProducts !== null && $this->product !== $this->aProducts->getId()) {
			$this->aProducts = null;
		}
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(StockdiaryPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = StockdiaryPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aLocations = null;
			$this->aProducts = null;
		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(StockdiaryPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			StockdiaryPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(StockdiaryPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			StockdiaryPeer::addInstanceToPool($this);
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aLocations !== null) {
				if ($this->aLocations->isModified() || $this->aLocations->isNew()) {
					$affectedRows += $this->aLocations->save($con);
				}
				$this->setLocations($this->aLocations);
			}

			if ($this->aProducts !== null) {
				if ($this->aProducts->isModified() || $this->aProducts->isNew()) {
					$affectedRows += $this->aProducts->save($con);
				}
				$this->setProducts($this->aProducts);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = StockdiaryPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += StockdiaryPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aLocations !== null) {
				if (!$this->aLocations->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aLocations->getValidationFailures());
				}
			}

			if ($this->aProducts !== null) {
				if (!$this->aProducts->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aProducts->getValidationFailures());
				}
			}


			if (($retval = StockdiaryPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}



			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = StockdiaryPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		$field = $this->getByPosition($pos);
		return $field;
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getDatenew();
				break;
			case 2:
				return $this->getReason();
				break;
			case 3:
				return $this->getLocation();
				break;
			case 4:
				return $this->getProduct();
				break;
			case 5:
				return $this->getUnits();
				break;
			case 6:
				return $this->getPrice();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = StockdiaryPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getDatenew(),
			$keys[2] => $this->getReason(),
			$keys[3] => $this->getLocation(),
			$keys[4] => $this->getProduct(),
			$keys[5] => $this->getUnits(),
			$keys[6] => $this->getPrice(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = StockdiaryPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setDatenew($value);
				break;
			case 2:
				$this->setReason($value);
				break;
			case 3:
				$this->setLocation($value);
				break;
			case 4:
				$this->setProduct($value);
				break;
			case 5:
				$this->setUnits($value);
				break;
			case 6:
				$this->setPrice($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = StockdiaryPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setDatenew($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setReason($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setLocation($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setProduct($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setUnits($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setPrice($arr[$keys[6]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(StockdiaryPeer::DATABASE_NAME);

		if ($this->isColumnModified(StockdiaryPeer::ID)) $criteria->add(StockdiaryPeer::ID, $this->id);
		if ($this->isColumnModified(StockdiaryPeer::DATENEW)) $criteria->add(StockdiaryPeer::DATENEW, $this->datenew);
		if ($this->isColumnModified(StockdiaryPeer::REASON)) $criteria->add(StockdiaryPeer::REASON, $this->reason);
		if ($this->isColumnModified(StockdiaryPeer::LOCATION)) $criteria->add(StockdiaryPeer::LOCATION, $this->location);
		if ($this->isColumnModified(StockdiaryPeer::PRODUCT)) $criteria->add(StockdiaryPeer::PRODUCT, $this->product);
		if ($this->isColumnModified(StockdiaryPeer::UNITS)) $criteria->add(StockdiaryPeer::UNITS, $this->units);
		if ($this->isColumnModified(StockdiaryPeer::PRICE)) $criteria->add(StockdiaryPeer::PRICE, $this->price);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(StockdiaryPeer::DATABASE_NAME);

		$criteria->add(StockdiaryPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setId($this->id);

		$copyObj->setDatenew($this->datenew);

		$copyObj->setReason($this->reason);

		$copyObj->setLocation($this->location);

		$copyObj->setProduct($this->product);

		$copyObj->setUnits($this->units);

		$copyObj->setPrice($this->price);


		$copyObj->setNew(true);

	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new StockdiaryPeer();
		}
		return self::$peer;
	}

	
	public function setLocations(Locations $v = null)
	{
		if ($v === null) {
			$this->setLocation(NULL);
		} else {
			$this->setLocation($v->getId());
		}

		$this->aLocations = $v;

						if ($v !== null) {
			$v->addStockdiary($this);
		}

		return $this;
	}


	
	public function getLocations(PropelPDO $con = null)
	{
		if ($this->aLocations === null && (($this->location !== "" && $this->location !== null))) {
			$c = new Criteria(LocationsPeer::DATABASE_NAME);
			$c->add(LocationsPeer::ID, $this->location);
			$this->aLocations = LocationsPeer::doSelectOne($c, $con);
			
		}
		return $this->aLocations;
	}

	
	public function setProducts(Products $v = null)
	{
		if ($v === null) {
			$this->setProduct(NULL);
		} else {
			$this->setProduct($v->getId());
		}

		$this->aProducts = $v;

						if ($v !== null) {
			$v->addStockdiary($this);
		}

		return $this;
	}


	
	public function getProducts(PropelPDO $con = null)
	{
		if ($this->aProducts === null && (($this->product !== "" && $this->product !== null))) {
			$c = new Criteria(ProductsPeer::DATABASE_NAME);
			$c->add(ProductsPeer::ID, $this->product);
			$this->aProducts = ProductsPeer::doSelectOne($c, $con);
			
		}
		return $this->aProducts;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
		} 
			$this->aLocations = null;
			$this->aProducts = null;
	}

} 